<?php
namespace Admin\Controller;
use Think\Controller;
class QuestionController extends CommonController {
    public function index(){
    	
        $this->show();
    }

 //问题列表
    public function question_list(){
           // 分页
      $count= M('question')->count();// 查询满足要求的总记录数
      $Page = new \Think\Page($count,8);// 实例化分页类 传入总记录数和每页显示的记录数(8)
      $show  = $Page->show();// 分页显示输出

      $this->assign('page',$show);// 赋值分页输出 
   
            // 问题
      $sql="select t1.*,t2.cname from  question t1 inner join category t2  on t1.cid=t2.id order by t1.id desc limit ".$Page->firstRow.','.$Page->listRows ;
       $question=M()->query($sql);

       $this->assign('question',$question);
    	 $this->show();
    }
  // 删除问题
  public function question_del(){
  
     $this->show();
  }
  // 编辑问题
  public function question_edit(){
    $id=I('get.id');
    $sql="select t1.*,t2.cname from  question t1 inner join category t2 on t1.cid=t2.id  where t1.id='{$id}'" ;
    $res=M()->query($sql);
    $res=current($res);
    $category=D('category')->getCates();//取出分类

    $this->assign('category',$category);
    $this->assign('res',$res);
    $this->show();
  }
  // 核对编辑问题
   public function checkedit(){
     $data['title']=I('post.title');
     $data['content']=I('post.content');
     $data['cid']=I('post.cid');
     $data['uid']=I('post.uid');
     $data['id']=I('post.id');
     // var_dump($data);
    if (M('question')->save($data)) {
       $arr=array(
          'error'=>1,

          'info'=>'保存成功'
        );
       echo json_encode($arr);
       exit;
    }else{
      $arr=array(
          'error'=>2,
          'info'=>'保存失败'
        );
       echo json_encode($arr);
       exit;
    }
   }
  // 删除问题
  public function question_dele(){
        $id=I('post.id');
        if (M('question')->delete($id)) {
          $arr=array(
              'error'=>1,
              'info'=>'删除成功'
            );
          echo json_encode($arr);
          exit;
        }else{
           $arr=array(
              'error'=>2,
              'info'=>'删除失败'
            );
          echo json_encode($arr);
          exit;
        }
  }
  
  // 批量删除问题
  public function delAll(){
       $str=I('post.str');
       $str=rtrim($str,",");
       $sql="delete from question where id in (".$str.")";
       // var_dump($sql);
       if (M()->execute($sql)) {
         $arr=array(
          'error'=>1,
          'info'=>'批量删除成功'
          );
         echo json_encode($arr);
         exit;
       }else{
            $arr=array(
          'error'=>2,
          'info'=>'批量删除失败'
          );
         echo json_encode($arr);
         exit;
       }



  }
  // 增加问题
  public function question_add(){
    // var_dump(I('post.'));
     $category=D('category')->getCates();
     // var_dump($category);
     $this->assign('category',$category);

    $this->show();
  }

  // 核对增加问题
  public function question_checkadd(){
    $data['title']=I('post.title');
    $data['content']=I('post.content');
    $data['cid']=I('post.cid');
    $data['uid']=$_SESSION['id'];
    $data['create_time']=time();
    // var_dump($data);
    if (M('question')->add($data)) {
      $arr=array(
        'error'=>1,
        'info'=>'添加成功'
        );
      echo json_encode($arr);
      exit;
    }else{
      $arr=array(
        'error'=>2,
        'info'=>'添加失败'
        );
      echo json_encode($arr);
      exit;
    }

  }
}
